package com.milci.ebusinesssuite;

import java.sql.*;
import java.util.*;

/**
 *
 *  Copyright (C) 2007 Mehmet Milci
 *                 ebusinesssuite-dev@milci.com
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
public class eBusinessSuiteFndAuth{

    private String userId;
    private String password;
    private String dbstring;
    private String gwuname;
    private String gwupass;
    private String fndname;
    private String dbserverId;
    
    private Connection appsCon = null;
    
    public eBusinessSuiteFndAuth(String dbstring, String gwuname, String gwupass, String fndname,String dbserverId) {
       this.dbstring= dbstring;
       this.gwuname= gwuname;
       this.gwupass= gwupass;
       this.fndname= fndname;
       this.dbserverId=dbserverId;
    }

    public void setUsername(String userId) {
       this.userId = userId;
    }

    public void setPassword(String password) {
       this.password = password;
    }

    public boolean Authenticate() {
    boolean control = false;
    try {
      try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
      }
      catch (ClassNotFoundException ex) {
        System.err.println("eBusinessSuite authentication (ClassNotFoundException ): " + ex.toString());
        return false;
      }
      Connection con = DriverManager.getConnection("jdbc:oracle:thin:@"+dbstring,gwuname,gwupass);
      CallableStatement proc = con.prepareCall("{ call FND_SECURITY_PKG.fnd_encrypted_pwd(?,?,?,?) }");
      proc.setString(1, userId.toUpperCase(Locale.US));
      proc.setString(2, dbserverId);
      proc.registerOutParameter(3, Types.INTEGER);
      proc.registerOutParameter(4, Types.VARCHAR);
      proc.execute();
      int user_id = proc.getInt(3);
      String ep = proc.getString(4);
      control = eBusinessSuiteSecurity.check(userId.toUpperCase(Locale.US) + "/" + password.toUpperCase(Locale.US), ep, false);
      proc.close();
      con.close();
    }
    catch (SQLException ex) {
      control = false;
      if (ex.getErrorCode() == 0) {
        System.err.println("eBusinessSuite authentication (SQLException): " + ex.toString());
      }
      else if (ex.getErrorCode() == 1017) {
        System.err.println("eBusinessSuite authentication (SQLException): Invalid GWYUID");
      }
      else if (ex.getErrorCode() == 17002) {
        System.err.println("eBusinessSuite authentication (SQLException): Invalid Host,Port or SID");
      }
      else if (ex.getErrorCode() == 20001) {
        System.err.println("eBusinessSuite authentication: " + userId.toUpperCase(Locale.US) + " is not an application user");
      }
      else {
        System.err.println("eBusinessSuite authentication (SQLException): " + ex.getMessage());
      }
    }
    return control;
    }
}
